﻿<?php 

		// header('Access-Control-Allow-Origin: *'); //允许跨域
		header("Content-type: text/html; charset=utf-8");

		switch ($_POST['type']) {

 			case '32':
 				 echo selectTell($_POST['tells']);
 				break;
 			case '33':
 				 echo selectTellPass();
 				break;
 			case '34':
 				 echo selectTellDB();
 				break;
 			case '35':
 				 tellupLoadFile($_POST['uploadfile'],$_POST['sqlTable']);
 				break;
 			case '36':
				mpotTell();
 				break;
 			case '37':
 				 yxTellUpdate($_POST['xyTell']);
 				break;
 			case '38':
 				echo selectYxTellDB();
 				break;
 			case '39':
 				echo piLiangSubmit();
 			case '40':
 				echo selectCallShuJu();
 				break;
 			case '41':
 				echo selectToke();
 				break;
 			case '42':
 				echo setToke($_POST['name_i'],$_POST['toke']);
 				break;
 			case '43':
 				echo selectProgress();
 				break;
 			case '44':
 				echo selectQudaoZhuanDanCount();
 				break;
 			case '45':
 				echo selectGaiBiaoQianCount();
 				break;
 			case '46':
 				echo get591tell();
 				break;
			default:
				break;
		}
	
	

function upLoadFile($upLoadFile,$sqlTable){  //30



	$fileName=$_FILES['file']['name'];

	//  vnd.ms-excel
	$file_type = $_FILES['file']['type'];

	//var_dump($file_type);


	if ($file_type=='application/octet-stream' || $file_type=='application/vnd.ms-excel'){

		$uploadfile='excel/up/'.$upLoadFile;
		
		if(move_uploaded_file($_FILES['file']['tmp_name'],$uploadfile)){

			

			$sqlResult=upExcelSql($uploadfile,$sqlTable);


			$res['code'] = '0';
			$res['success']  = $sqlResult;
			$res_json=die(json_encode($res));



		}else{


			$res['code'] = '0';
			$res['success']  = '移动目录失败请检查代码!';
			$res_json=die(json_encode($res));


		}
	

	}elseif($file_type=='application/json'){



			$uploadfile='json/'.$upLoadFile;

			if(move_uploaded_file($_FILES['file']['tmp_name'],$uploadfile)){


				$sqlResult=upDataTableHuaTiId().'<br>'.getHuatiPinDaoId();

			
				$res['code'] = '0';
				$res['success']  = $sqlResult;
				$res_json=die(json_encode($res));

			}else{


				$res['code'] = '0';
				$res['success']  = '移动目录失败请检查代码!';
				$res_json=die(json_encode($res));


			}


	}else{

		$res['code'] = '0';
		$res['success']  = '上传失败，只能上传xls或xlsx格式!';
		$res_json=die(json_encode($res));
	}



	echo $res_json;



}

function upExcelSql($upLoadFilePath,$sqlTable){ //30-1




	require_once 'PHPExcel-1.8/Classes/PHPExcel.php';

	require_once 'PHPExcel-1.8/Classes/PHPExcel/IOFactory.php';

	$objPHPExcel =PHPExcel_IOFactory::load($upLoadFilePath);

	$dataArray=$objPHPExcel->getActiveSheet()->toArray();



	$host='localhost';
	$user='root';
	$password='root';
	$database='weizandata';
	$link=mysqli_connect($host, $user, $password, $database);
	$link->query("set names utf8");
    $erro='';
	$E=0;
	$S=0;
	
	
	for($i=1;$i<count($dataArray);$i++){


		foreach($dataArray[$i] as $key=>$value){

			$value=preg_replace("/'|\"/i",'',$value);

			$value="'{$value}'";

			$str[$key]= $value;

		}

		$qure='insert into '.$sqlTable.' value ('.implode($str,',').');';



		$result=mysqli_query($link, $qure);

		if($result){
			
			$S++;

		}else{
			
			$E++;

			$erro.= '语句：'.$qure.'<br>错误代码: '.mysqli_errno($link).'<br> 错误解释: '.mysqli_error($link).'<br>';

		}	

	}

	mysqli_close($link);

	if($E>0){

		return '成功插入: '.$S.'条'.'<br>失败: '.$E.' 条<br>'.'失败原因: <br>'.$erro;

	}else{

		return '成功插入: '.$S.'条';


	}



}

function selectTell($tell){  //32

	

	$tells1=json_decode($tell,true);

	$url='http://api.ip138.com/mobile/';

	$tableStr=[];

	$datatype = 'jsonp';

	$header = array('token:02e3640b98286e5c4600c1a7483fb073');



	foreach ($tells1 as $key => $value) {
	

		$mobile = $value['tell'];

		//$mobile = '18801263882';

		$url = 'http://api.ip138.com/mobile/?mobile='.$mobile.'&datatype='.$datatype;

		$dizhi=json_decode(getData($url,$header),true)['data'];

		array_push($tableStr, ['tell'=>$value['tell'],'province'=>$dizhi[0],'city'=>$dizhi[1],'isp'=>$dizhi[2]]);
	}

	


	$json = json_encode(array(
			"code"=>0,
			"msg"=>"",
			"data"=>$tableStr ),
			JSON_UNESCAPED_UNICODE);

	//var_dump($tableStr); 
	
	mysqli_close($link);


	return $json;



 }

function getData($url,$header){  //32-1

	$ch = curl_init();  
	curl_setopt($ch,CURLOPT_URL,$url);
	curl_setopt($ch,CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_1);
	curl_setopt($ch,CURLOPT_HTTPHEADER,$header); 
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);  
	curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,3);  
	$handles = curl_exec($ch);  
	curl_close($ch);  
	return $handles;  
}


function selectTellPass(){ //33


	$qure="select * from tellSelectPassWord";
	$host='localhost';
	$user='root';
	$password='root';
	$database='weizandata';
	$link=mysqli_connect($host, $user, $password, $database);
	$link->query("set names utf8");

	$result=mysqli_query($link, $qure);
	$arr=mysqli_fetch_all($result, 1);
	$jsonData=json_encode($arr);

	
	return $jsonData;
	mysqli_close($link);


}


function selectTellDB(){  //34

	$starTime=$_POST['starTime'];
	$endTime=$_POST['endTime'];
	$page=$_POST['page'];
	$limit=$_POST['limit'];

	$offset=($page-1)*$limit;


	$qure="SELECT * FROM  everyDayTell WHERE riqi BETWEEN '{$starTime}' AND '{$endTime}' LIMIT {$limit} OFFSET {$offset} ;";
	$host='localhost';
	$user='root';
	$password='root';
	$database='weizandata';
	$link=mysqli_connect($host, $user, $password, $database);
	$link->query("set names utf8");
	$result=mysqli_query($link, $qure);
	$arr=mysqli_fetch_all($result, 1);
	$jsonData=json_encode($arr);
	$json = json_encode(array(
			"code"=>0,
			"msg"=>"offset:".$offset,
			"count"=>selectTellDB_count(),
			"data"=>$arr ),
			JSON_UNESCAPED_UNICODE);
	
	mysqli_close($link);


	return $json;

}

function selectCallShuJu(){  //40

	$page=($_POST['page']-1)*$_POST['limit'];

	$limit=$_POST['limit'];

	$qure="CALL submitCount({$limit},{$page});";
	$qure1="CALL submitCountSum();";
	$host='localhost';
	$user='root';
	$password='root';
	$database='weizandata';
	$link=mysqli_connect($host, $user, $password, $database);
	$link1=mysqli_connect($host, $user, $password, $database);
	$link->query("set names utf8");
	$link1->query("set names utf8");
	$result=mysqli_query($link, $qure);
	$result1=mysqli_query($link1, $qure1);
	$arr=mysqli_fetch_all($result, 1);
	$arr1=mysqli_fetch_all($result1, 1);

	$jsonData=json_encode($arr);
	$json = json_encode(array(
			"code"=>0,
			"msg"=>"",
			"count"=>$arr1[0]['sum'],
			"data"=>$arr ),
			JSON_UNESCAPED_UNICODE);
	
	mysqli_close($link);

	return $json;

}

function selectTellDB_count(){  //计算条目数

	$starTime=$_POST['starTime'];
	$endTime=$_POST['endTime'];
	$qure="SELECT * FROM  everyDayTell WHERE riqi BETWEEN '{$starTime}' AND '{$endTime}';";
	$host='localhost';
	$user='root';
	$password='root';
	$database='weizandata';
	$link=mysqli_connect($host, $user, $password, $database);
	$link->query("set names utf8");
	$result=mysqli_query($link, $qure);
	$arr=mysqli_fetch_all($result, 1);

	return count($arr);

}

function tellupLoadFile($upLoadFile,$sqlTable){  //35



	$fileName=$_FILES['file']['name'];

	
	$file_type = $_FILES['file']['type'];

	if ($file_type=='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' ||$file_type=='application/octet-stream'){

		$uploadfile='excel/up/'.$upLoadFile;
		
		if(move_uploaded_file($_FILES['file']['tmp_name'],$uploadfile)){
			

			$sqlResult=tellupLoadFile_insertDB($uploadfile,$sqlTable);


			$res['code'] = '0';
			$res['success']  =$sqlResult;
			$res_json=die(json_encode($res));

		}else{


			$res['code'] = '0';
			$res['success']  = '移动目录失败请检查代码!';
			$res_json=die(json_encode($res));

		}

	}else{

		$res['code'] = '0';
		$res['success']  = '上传失败，只能上传xls或xlsx格式!';
		$res_json=die(json_encode($res));
	}

	echo $res_json;

}



function tellupLoadFile_insertDB($upLoadFilePath,$sqlTable){  //35-1


			require_once 'PHPExcel-1.8/Classes/PHPExcel.php';

			require_once 'PHPExcel-1.8/Classes/PHPExcel/IOFactory.php';

			$objPHPExcel =PHPExcel_IOFactory::load($upLoadFilePath);

			$dataArray=$objPHPExcel->getActiveSheet()->toArray();

			switch (1) {

				case 0:
					
					$host='localhost';
					$user='root';
					$password='root';
					$database='weizandata';
					$link=mysqli_connect($host, $user, $password, $database);
					$link->query("set names utf8");
				    $erro='';
					$E=0;
					$S=0;
					$C=0;


					$linkE=mysqli_connect($host, $user, $password, $database);

					$linkE->query("set names utf8");

			
					for($i=1;$i<count($dataArray);$i++){


						foreach($dataArray[$i] as $key=>$value){

							$value=preg_replace("/'|\"/i",'',$value);

							$value1="'{$value}'";

							$str[$key]= $value1;	

							 // $str[1]="'".tellupLoadFile_selectInfo($value)[0]."'";  //用API方法

							 // $str[2]="'".tellupLoadFile_selectInfo($value)[1]."'"; //用API方法

							$str[1]="'".tellupLoadFile_DBselectInfo($value)[0]."'"; //用本地数据库方法

							$str[2]="'".tellupLoadFile_DBselectInfo($value)[1]."'"; //用本地数据库方法

							$str[3]="'".date('Y-m-d')."'";
			

						}


						$qure='insert into '.$sqlTable.' value ('.implode($str,',').');';

						$result=mysqli_query($link, $qure);

						if($result){
							
							$S++;

						}else{
							
							

							if(mysqli_errno($link)=='1062'){

								$C++;

								$qure='insert into chongfuTell value ('.implode($str,',').');';

								mysqli_query($linkE, $qure);


							}else{

								$E++;

								$erro.= '语句：'.$qure.'<br>错误代码: '.mysqli_errno($link).'<br> 错误解释: '.mysqli_error($link).'<br>';


							}

							

						}	

					}

					mysqli_close($link);

					if($E>0){

						return '成功插入: '.$S.'条'.'<br>失败: '.$E.' 条<br>'.'<br>重复: '.$C.' 条<br>'.'失败原因: <br>'.$erro;

					}else{

						return '成功插入: '.$S.'条'.'<br>重复: '.$C.' 条<br>';


					}

			break;

		case 1:
				
			return piliangChaXunTell($dataArray);

			break;

			}





}


function tellupLoadFile_selectInfo($mobile){  //35-2用API方式


			$datatype = 'jsonp';

			$header = array('token:02e3640b98286e5c4600c1a7483fb073');

			$url = "http://api.ip138.com/mobile/?mobile={$mobile}&datatype={$datatype}";

			$dizhi=json_decode(getData($url,$header),true)['data'];

			return $dizhi;
}



function tellupLoadFile_DBselectInfo($mobile){  //35-3用数据库方式


	$mobile=substr($mobile,0,7);



	$qure="SELECT province,city FROM  phone WHERE phone = '{$mobile}';";
	$host='localhost';
	$user='root';
	$password='root';
	$database='phone';
	$link=mysqli_connect($host, $user, $password, $database);
	$link->query("set names utf8");
	$result=mysqli_query($link, $qure);
	$arr=mysqli_fetch_all($result, 1);

	$arr1=[$arr[0]['province'],$arr[0]['city']];


	mysqli_close($link);


	return $arr1;

			
}

function piliangChaXunTell($arrTell){  //35-3批量查询电话归属地


	$arrPhoe=array();



	$n=0;

	for($i=1;$i<count($arrTell);$i++){


		foreach($arrTell[$i] as $key=>$value){

			$value=preg_replace("/'|\"/i",'',$value);

			$value1="'".substr($value, 0,7)."'";

			$arrPhoe[$n]=$value1;

		}

		$n++;

	}


	$str="(".implode($arrPhoe,',').")";



	$qure='SELECT phone,province,city FROM phone WHERE phone IN '.$str;
	$host='localhost';
	$user='root';
	$password='root';
	$database='phone';
	$link=mysqli_connect($host, $user, $password, $database);
	$link->query("set names utf8");
	$result=mysqli_query($link, $qure);
	$arr=mysqli_fetch_all($result, 1);


	mysqli_close($link);



	$phoneArr=array();

	$a=0;

	for($i=1;$i<count($arrTell);$i++){

		foreach($arrTell[$i] as $key=>$value){

			$value=preg_replace("/'|\"/i",'',$value);

			$valueLeft=substr($value, 0,7);

			//echo $valueLeft.'<br><hr>';

			for($j=0;$j<count($arr);$j++){


				$e=0;


				if($arr[$j]['phone']==$valueLeft){

					//echo $valueLeft.':'.$arr[$j]['phone'].'<br>';

					$phoneArr[$a]="('".$value."','".$arr[$j]['province']."','".$arr[$j]['city']."','".date('Y-m-d')."')";
					//$phoneArr[$a]="('".$value."','".$arr[$j]['province']."','".$arr[$j]['city']."','2020/7/2')";

					$a++;

					$e=1;

					break;

				}


			}

			if($e==0){


				$phoneArr[$a]="('".$value."','未知','未知','".date('Y-m-d')."')";

				//$phoneArr[$a]="('".$value."','未知','未知','2020/7/2')";
			}

		}


	}




	$qure1= "insert ignore into everydaytell (tell,shengFen,city,riqi) VALUES ".implode($phoneArr,',');

	//var_dump($qure1);
	
	$link1=mysqli_connect($host, $user, $password, 'weizandata');
	$link1->query("set names utf8");

	// 关闭自动提交 
	mysqli_autocommit($link1,FALSE); 

	$result1=mysqli_query($link1, $qure1);	

	

	$s=mysqli_affected_rows($link1);

	$aa=mysqli_warning_count($link1);


	if($aa>0){


		$chonfustr=piliangChaXunTellChongFu($arrTell);

	}


	mysqli_commit($link1);

	mysqli_close($link1);




	return '成功插入: '.$s.'条'.'<br>重复: '.$aa.' 条<br>'.$chonfustr;

	

}

function piliangChaXunTellChongFu($arrTell){   //35-4 批量查询电话归属地



	$qure='SELECT tell FROM everydaytell;';
	$host='localhost';
	$user='root';
	$password='root';
	$database='weizandata';
	$link=mysqli_connect($host, $user, $password, $database);
	$link->query("set names utf8");
	$result=mysqli_query($link, $qure);
	$arr=mysqli_fetch_all($result, 1);  //获取历史电话数据

	//var_dump($arrTell);

	$arr1=array(); //定义当天数据排重数组转成一维数组

	$arr2=array(); //定义历史排重数组成一维数组

	$chongFu=array();//将所有重复数组收集到一起

	foreach ($arrTell as $key => $value) {
		

		$arr1[$key]= "'".$value[0]."'";

	}

	$itSum=0;

	$countI=0;

	foreach (array_count_values($arr1) as $key1 => $value1) {
		
		if($value1>1){

			$iT=(int)$value1-1;

			$itSum=$itSum+$iT;

			for($ia=0;$ia<$iT;$ia++){

				$chongFu[$countI]=$key1;

				$countI++;

			}

			//echo $key1.'：重复：'.$iT.'次<br>';
		}
	}


	foreach ($arr as $key2 => $value2) {

		$arr2[$key2]="'".$value2['tell']."'";
		
	}


	$chongFuarr=array_intersect($arr1, $arr2);

	foreach ($chongFuarr as $key3 => $value3) {

		array_push($chongFu, $value3) ;
		
	}


	return selectGuiShuDi($chongFu);  //调用插入重复值函数


}

function selectGuiShuDi($tell){   //35-5批量插入重复查询电话归属地




	$leftQ=array();

	foreach ($tell as $key => $value) {
		

		$leftQ[$key]=substr($value, 0,8)."'";

	}

	$str=implode($leftQ,',');
	
	$qure="SELECT phone, province,city FROM phone WHERE phone in (".$str.");";
	$host='localhost';
	$user='root';
	$password='root';
	$database='phone';
	$link=mysqli_connect($host, $user, $password, $database);
	$link->query("set names utf8");
	$result=mysqli_query($link, $qure);
	$arr=mysqli_fetch_all($result, 1);  //获取历史电话数据



	foreach ($tell as $key => $value) {
		

		$leftQ1=substr($value, 1,7);


		foreach ($arr as $key1 => $value1) {

			$boi=0;

			if($value1['phone']==$leftQ1){


				$tell[$key]="(".$value.",'".$value1['province']."','".$value1['city']."','".date('Y-m-d')."')";

				$boi++;

				break;

			}
			
		}


		if($boi==0){

			$tell[$key]="(".$value.",'未知','未知','".date('Y-m-d')."')";
				
		}

	}




		$qure1="INSERT INTO chongfutell VALUE ".implode($tell,',').";";
		$host='localhost';
		$user='root';
		$password='root';
		$database='weizandata';
		$link1=mysqli_connect($host, $user, $password, $database);
		$link1->query("set names utf8");
		$result1=mysqli_query($link1, $qure1);


		if($result1){

			return '成功插入重复数据';

		}else{

			return  mysqli_error($link1);
		}

		//var_dump($result1);

		//var_dump(mysqli_error($link1));




}

function yxTellUpdate($arr){ //37


	$tell=json_decode($arr,true);

	//var_dump($tell);

	$host='localhost';
	$user='root';
	$password='root';
	$database='weizandata';

	$yxtell=$tell['tell'];

	$s=0;


	for($i=0;$i<count($yxtell);$i++){

		$qure="SELECT yxRiqi FROM everydaytell WHERE tell = '".$yxtell[$i]."';";

		//echo $qure.'<br>';

		$link=mysqli_connect($host, $user, $password, $database);
		$link->query("set names utf8");
		$result=mysqli_query($link, $qure);
		$arr=mysqli_fetch_all($result, 1);  //获取历史电话数据

		//var_dump( $arr[0]['yixiang']);

		if (is_null($arr[0]['yxRiqi'])) {


				$qure1="UPDATE everydaytell SET yxRiqi='".date('Y-m-d H:i:s')."' WHERE tell='".$yxtell[$i]."'";

				$link1=mysqli_connect($host, $user, $password, $database);
				$link1->query("set names utf8");
				$result1=mysqli_query($link1, $qure1);

				if ($result1) {
					$s++;
				}

			
		}

		

	}



	$sus="成功更新".$s."条";

	echo  $sus;

}
function selectYxTellDB(){  //38

	$starTime=$_POST['starTime'];
	$endTime=$_POST['endTime'];

	

	$qure="SELECT * FROM  everyDayTell WHERE riqi BETWEEN '{$starTime}' AND '{$endTime}';";

	//echo $qure;
	$host='localhost';
	$user='root';
	$password='root';
	$database='weizandata';
	$link=mysqli_connect($host, $user, $password, $database);
	$link->query("set names utf8");
	$result=mysqli_query($link, $qure);
	$arr=mysqli_fetch_all($result, 1);
	$jsonData=json_encode($arr);
	$json = json_encode(array(
			"code"=>0,
			"msg"=>count($arr),
			"data"=>$arr ),
			JSON_UNESCAPED_UNICODE);
	
	
	mysqli_close($link);


	return $json;

}

function piLiangSubmit(){  //39


	@$token = file_get_contents("token.txt");


	$qure="SELECT * FROM everydaytell  WHERE yxRiqi IS NOT NULL AND submit IS NULL;";

	//echo $qure;
	$host='localhost';
	$user='root';
	$password='root';
	$database='weizandata';
	$link=mysqli_connect($host, $user, $password, $database);
	$link->query("set names utf8");
	$result=mysqli_query($link, $qure);
	$arr=mysqli_fetch_all($result, 1);

	$jsonData=json_encode($arr);
	$json = json_encode(array(
			"code"=>0,
			"msg"=>count($arr),
			"data"=>$arr ),
			JSON_UNESCAPED_UNICODE);
	
	
	mysqli_close($link);


	return $json;

}

function selectToke(){ //41

	$qure="SELECT * FROM toke;";

	$host='localhost';
	$user='root';
	$password='root';
	$database='weizandata';
	$link=mysqli_connect($host, $user, $password, $database);
	$link->query("set names utf8");
	$result=mysqli_query($link, $qure);
	$arr=mysqli_fetch_all($result, 1);
	$jsonData=json_encode($arr);
	$json = json_encode(array(
			"code"=>0,
			"msg"=>count($arr),
			"data"=>$arr ),
			JSON_UNESCAPED_UNICODE);
	
	
	mysqli_close($link);

	return $json;

}

function setToke($name_i,$toke){ //42

	$qure="UPDATE toke SET toke='{$toke}' WHERE name_i='{$name_i}';";

	$host='localhost';
	$user='root';
	$password='root';
	$database='weizandata';
	$link=mysqli_connect($host, $user, $password, $database);
	$link->query("set names utf8");
	$result=mysqli_query($link, $qure);
	$json = json_encode(array(
			"code"=>0,
			"msg"=>$result?'修改成功':'修改失败',
			"data"=>$result ),
			JSON_UNESCAPED_UNICODE);
	
	mysqli_close($link);

	return $json;

}

function selectProgress(){  //43

	$qure="SELECT a.show_i,b.total,a.incomplete FROM (SELECT '进度' AS show_i,COUNT(*) AS  incomplete FROM youjutell WHERE youjutell.`date_submit` IS NULL AND youjutell.`name` NOT IN ('万物591','河北591','鼎盛百度','自传播来源','开信河北','591-lx','test-lx')) a LEFT JOIN (SELECT '进度' AS show_i, COUNT(*) AS total FROM youjutell WHERE LEFT(youjutell.`start_date`,10) = CURDATE() AND youjutell.`name` NOT IN ('万物591','河北591','鼎盛百度','自传播来源','开信河北','591-lx','test-lx'))b ON a.show_i =  b.show_i;";

	$host='localhost';
	$user='root';
	$password='root';
	$database='weizandata';
	$link=mysqli_connect($host, $user, $password, $database);
	$link->query("set names utf8");
	$result=mysqli_query($link, $qure);
	$arr=mysqli_fetch_all($result, 1);
	$json = json_encode(array(
			"code"=>0,
			"msg"=>$result?'查询成功':'查询成功',
			"data"=>$arr ),
			JSON_UNESCAPED_UNICODE);
	
	mysqli_close($link);

	return $json;



}

function selectQudaoZhuanDanCount(){

	

	$qure="SELECT * FROM (SELECT LEFT(AssignTime,10) AS riqi,ChannelName,COUNT(*) AS COUNT FROM weizandata.`zhuandanphone` WHERE ChannelName IN (SELECT qudao_name FROM qudaoid) AND AssignTime BETWEEN DATE_SUB(NOW(),INTERVAL 1 WEEK) AND NOW() GROUP BY LEFT(AssignTime,10),ChannelName UNION ALL SELECT LEFT(AssignTime,10) AS riqi,'合计' AS Channe,COUNT(*) AS COUNT FROM weizandata.`zhuandanphone` WHERE ChannelName IN (SELECT qudao_name FROM qudaoid) AND AssignTime BETWEEN DATE_SUB(NOW(),INTERVAL 1 WEEK) AND NOW() GROUP BY LEFT(AssignTime,10)) a ORDER BY riqi DESC, COUNT;";

	$host='localhost';
	$user='root';
	$password='root';
	$database='weizandata';
	$link=mysqli_connect($host, $user, $password, $database);
	$link->query("set names utf8");
	$result=mysqli_query($link, $qure);
	$arr=mysqli_fetch_all($result, 1);
	$jsonData=json_encode($arr);
	$json = json_encode(array(
			"code"=>0,
			"msg"=>"",
			"data"=>$arr ),
			JSON_UNESCAPED_UNICODE);
	
	mysqli_close($link);

	return $json;


}

function selectGaiBiaoQianCount(){

	

	$qure="CALL gaibiaoqian();";

	$host='localhost';
	$user='root';
	$password='root';
	$database='weizandata';
	$link=mysqli_connect($host, $user, $password, $database);
	$link->query("set names utf8");
	$result=mysqli_query($link, $qure);
	$arr=mysqli_fetch_all($result, 1);
	$jsonData=json_encode($arr);
	$json = json_encode(array(
			"code"=>0,
			"msg"=>"",
			"data"=>$arr ),
			JSON_UNESCAPED_UNICODE);
	
	mysqli_close($link);

	return $json;


}

function get591tell(){

	if(isset($_POST['starTime']) && isset($_POST['endTime'])){

		// NAME in ('河北591') AND
		//$qure="SELECT * FROM youjutell WHERE NAME in ('河北591') AND date_submit BETWEEN '". $_POST['starTime']."' AND '".$_POST['endTime']."'  ORDER BY start_date DESC;";

		$qure="SELECT start_date,title,link_url,SUBSTRING(special,INSTR(special,'_')+1,LENGTH(special)) AS prv,SUBSTRING_INDEX(special,'_',1) AS city FROM youjutell WHERE NAME IN ('河北591','河北优居','河北鼎盛','开信河北') AND start_date BETWEEN '". $_POST['starTime']."' AND '".$_POST['endTime']."' ORDER BY start_date, city DESC;";

		// echo $qure;

	}else{

		// $qure="SELECT * FROM youjutell WHERE NAME in ('河北591') ORDER BY start_date DESC;";

		$qure="SELECT start_date,title,link_url,SUBSTRING(special,INSTR(special,'_')+1,LENGTH(special)) AS prv,SUBSTRING_INDEX(special,'_',1) AS city FROM youjutell WHERE NAME IN ('河北591') ORDER BY start_date DESC, city DESC;";

	}

	// echo $qure;

	$host='localhost';
	$user='root';
	$password='root';
	$database='weizandata';
	$link=mysqli_connect($host, $user, $password, $database);
	$link->query("set names utf8");
	$result=mysqli_query($link, $qure);
	$arr=mysqli_fetch_all($result, 1);
	$jsonData=json_encode($arr);
	$json = json_encode(array(
			"code"=>0,
			"msg"=>"",
			"data"=>$arr ),
			JSON_UNESCAPED_UNICODE);
	
	mysqli_close($link);

	return $json;


}

function  php_do_url_POST($url,$params,$headers){ //POST get

	$ch = curl_init ();

	curl_setopt ( $ch, CURLOPT_URL, $url );
	curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
	curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, 'POST' );
	curl_setopt ( $ch, CURLOPT_POSTFIELDS, $params );
	curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
	curl_setopt ( $ch, CURLOPT_TIMEOUT, 60 );
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
	curl_setopt($ch, CURLOPT_SSLVERSION, false);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
	$json_string = curl_exec ( $ch );
	$data = json_decode($json_string, true);
	curl_close ( $ch );
	return $data;
}
function php_do_Url_GET($url,$params){ //Get get

	$url = "{$url}?" . http_build_query ($params);
	$ch = curl_init ();
	curl_setopt ( $ch, CURLOPT_URL, $url );
	curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
	curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, 'GET' );
	curl_setopt ( $ch, CURLOPT_TIMEOUT, 60 );
	curl_setopt ( $ch, CURLOPT_POSTFIELDS, $params );
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
	curl_setopt($ch, CURLOPT_SSLVERSION, false);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);	

	$json_string = curl_exec ( $ch );

	$data = json_decode($json_string, true);

	curl_close ( $ch );

	return $data;
}


function selectYeWuBu($type,$shengFen,$shi){

	$json_string = file_get_contents('json/shiYeBu.json');

	$data = json_decode($json_string, true);

	//var_dump($data['shiY1']);



	switch ($type) {

		case '1':

			$yeWuYiBu = $data['shengY1'];

			$erWuYiBu = $data['shengY2'];

			$sanWuYiBu =$data['shengY3'];

			$beiJing = $data['beijing'];

			for($i=0;$i<count($yeWuYiBu);$i++){

				$a=preg_match("/{$yeWuYiBu[$i]['sheng']}/i",$shengFen);

				if($a>0){ 


					if(preg_match("/天津/i",$shengFen)){


						if(preg_match("/蓟县|蓟州|宝坻/i", $shi)){

							return "国内业务二部";
						}else{

							return "国内业务一部";
						}
						

					}else{

						return "国内业务一部"; 

					}

					exit;


				};

			}

			for($i=0;$i<count($erWuYiBu);$i++){

				$a=preg_match("/{$erWuYiBu[$i]['sheng']}/i",$shengFen);

				if($a>0){ return "国内业务二部"; exit;};

			}

			for($i=0;$i<count($sanWuYiBu);$i++){

				$a=preg_match("/{$sanWuYiBu[$i]['sheng']}/i",$shengFen);

				if($a>0){ return "国内业务三部"; exit;};

			}	

			for($i=0;$i<count($beiJing);$i++){

				$a=preg_match("/{$beiJing[$i]['sheng']}/i",$shengFen);

				if($a>0){ 

					if($shi){

						if(preg_match("/房山/i",$shi)>0){

							return "国内业务二部";

						}else{

							return "北京"; 
						}

					}else{


						return "北京";

					}

					exit;

				}

			}

		
		
		break;

		case '2':

			$yeWuYiBu1 = $data['shiY1'];
			$erWuYiBu1 = $data['shiY2'];
			$sanWuYiBu1 = $data['shiY3'];
			$beiJing = $data['beijing'];


			for($i=0;$i<count($yeWuYiBu1);$i++){

				$a=preg_match("/{$yeWuYiBu1[$i]['city']}/i", $shi);

				if($a>0){

					if(preg_match("/天津/i",$shi)){

						if(preg_match("/蓟县|蓟州|宝坻/i",$shi)){

							return "国内业务二部";

						}else{

							return "国内业务一部";

						}
						
						exit;
						

					}else{

						return "国内业务一部";

					}


					exit;

				}


			}


			for($i=0;$i<count($erWuYiBu1);$i++){

					if(preg_match("/{$yeWuYiBu2[$i]['city']}/i", $shi)){

						return "国内业务二部";

						exit;

					}

			}


			for($i=0;$i<count($sanWuYiBu1);$i++){

					if(preg_match("/{$yeWuYiBu3[$i]['city']}/i", $shi)){

						return "国内业务三部";

						exit;

					}



			}

			for($i=0;$i<count($beiJing);$i++){

					if(preg_match("/{$beiJing[$i]['sheng']}/i", $shi)){


						if(preg_match("/房山/i",$shi)){

							return "国内业务二部";

						}else{

							return "北京";


						}

						exit;

					}



			}

			
			break;

	}




	

}

function scerweima($url=''){
  require_once 'phpqrcode/phpqrcode.php';
  $value = $url;         //二维码内容
  $errorCorrectionLevel = 'L';  //容错级别
  $matrixPointSize = 5;      //生成图片大小
  //生成二维码图片
  $filename = 'qrcode/'.microtime().'.png';
  QRcode::png($value,$filename , $errorCorrectionLevel, $matrixPointSize, 2);
  $QR = $filename;        //已经生成的原始二维码图片文件
  $QR = imagecreatefromstring(file_get_contents($QR));
  //输出图片
  imagepng($QR, 'qrcode.png');
  imagedestroy($QR);
  return '<img src="qrcode.png" alt="未加载">';
}


?>